import { BasicColumn, FormSchema } from '@/components/Table';
import { h, ref } from 'vue';
import { Tag } from 'ant-design-vue';
import { dictAllListApi, supplierListApi, materialListApi } from '@/api/sys/msUser';
import { ApiSelect } from '@/components/Form';
import util from '@/utils/util';
import FormSelectTable from '@/views/msManage/components/formSelectTable/index.vue';
import SelectSupplierIndex from '@/views/msManage/components/formSelectTable/supplier/SelectSupplierIndex.vue';
import SelectOrderIndex from '@/views/msManage/components/formSelectTable/order/SelectOrderIndex.vue';

import { useMessage } from '@/hooks/web/useMessage';
import Ainput from '@/views/bookManage/components/account/Ainput.vue';
import { VxeFormItemProps, VxeGridPropTypes } from '@/components/VxeTable';

export function openIsReturn(record: isReturn) {
  console.log('--openIsReturn ', record);
}

type CheckedType = boolean | string | number;
// BasicColumn[]

// VxeColumnPropTypes.CellRender

export const vxeTableColumns: VxeGridPropTypes.Columns = [
  {
    title: '型号',
    field: 'mouldName',
    showOverflow: 'title',
    filters: [{ data: '' }],
    filterRender: {
      name: 'VxeInput',
    },
    width: 140,
  },
  {
    title: '物料名称',
    field: 'materialName',
    showOverflow: 'title',
    filters: [{ data: '' }],
    filterRender: {
      name: 'VxeInput',
    },
    width: 140,
  },
  {
    title: '物料规格',
    field: 'specification',
    showOverflow: 'title',
    filters: [{ data: '' }],
    filterRender: {
      name: 'VxeInput',
    },
    width: 140,
  },
  {
    title: '虚拟件单号',
    field: 'orderNo',
    align: 'center',
    width: 120,

  },
  {
    title: '金额',
    field: 'price',
    align: 'center',
    width: 100,
  },

  {
    title: '供应商名称',
    field: 'supplierName',
    showOverflow: 'title',
    width: 200,

    filters: [{ data: '' }],
    filterRender: {
      name: 'VxeInput',
    },
  },

  {
    title: '开始日期',
    field: 'startDate',
    align: 'center',
    // sortable: true,
    width: 100,
  },
  {
    title: '结束日期',
    field: 'endDate',
    align: 'center',
    // sortable: true,
    width: 100,
  },
  {
    title: '退款条件类别',
    field: 'returnConditionType',
    align: 'center',
    width: 90,
    slots: {
      default: ({ row }) => {
        const type = row.returnConditionType;
        let text = '';
        switch (type) {
          case 1:
            text = '一次性(数量)';
            break;
          case 2:
            text = '累计(数量)';
            break;
          case 3:
            text = '不退';
            break;
          case 4:
            text = '一次性(金额)';
            break;
          case 5:
            text = '累计(金额)';
            break;
        }
        // return text;
        return h(Tag, null, () => text);
      },
    },
  },
  {
    title: '退款条件数量/金额',
    width: 80,
    field: 'returnConditionNum',
  },
  {
    title: '已累计数量下单数量/金额',
    width: 80,
    field: 'totalNum',
  },
  {
    title: 'ms系统累计下单量',
    field: 'msTotalNum',
    align: 'center',
    width: 100,
    slots: { default: 'msTotalNum_default' },
  },
  {
    title: '是否符合退款条件',
    field: 'isReturn',
    align: 'center',
    width: 100,
    slots: { default: 'isReturn_default' },
  },
  {
    title: '是否关闭',
    field: 'isClose',
    align: 'center',

    width: 80,
    slots: {
      default({ row }) {
        const type = row.isClose;
        let text = '';
        let color = '';
        switch (type) {
          case 0:
            text = '否';
            break;
          case 1:
            text = '是';
            color = 'red';
            break;
        }
        return h(Tag, { color }, () => text);
      },
    },
  },
  // {
  //   title: '人员类型',
  //   dataIndex: 'userType',
  //   width: 100,
  //   align: 'center',
  //   slots: {
  //     default: ({ row }) => {
  //       const type = row.userType;
  //       let text="";
  //       switch (type) {
  //         case 1:
  //           text="采购员";
  //           break;
  //         case 2:
  //           text="研发制样员";
  //           break;
  //       }
  //       // return text;
  //       return h(Tag, null, () => text);
  //     },
  //   }
  // },
  {
    title: '经办人',
    field: 'userName',
    showOverflow: 'title',
    align: 'center',
    filters: [{ data: '' }],
    filterRender: {
      name: 'VxeInput',
    },
    width: 100,
  },
  {
    title: '提请人',
    field: 'createUserName',
    align: 'center',

    width: 100,
  },
  {
    title: '创建时间',
    field: 'createTime',
    align: 'center',

    width: 150,
  },
  {
    width: 150,
    title: '操作',
    align: 'center',
    field: '',
    slots: { default: 'action' },
    fixed: 'right',
  },
];

export const vxeTableFormSchema: VxeFormItemProps[] = [
  {
    span: 6,
    field: 'keyword',
    title: '查询',
    itemRender: {
      props: {
        placeholder: '型号/物料名称/物料规格/虚拟件单号',
      },
      name: 'AInput',
    },
  },
  {
    span: 5,
    field: 'isClose',
    title: '是否关闭',
    itemRender: {
      name: 'ASelect',
      defaultValue: 0,
      props: {
        options: [
          { label: '是', value: 1 },
          { label: '否', value: 0 },
        ],
      },
    },
  },
  {
    span: 5,
    itemRender: {
      name: 'VxeButtonGroup',
      options: [
        { type: 'reset', content: '重置' },
        { type: 'submit', content: '查询', status: 'primary' },
      ],
    },
  },
];

export const rules = {
  mouldNameObj: [{ required: true, message: '费用名称不能为空', trigger: 'change' }],
  materialNo: [
    { required: true, message: '物料编号不能为空', trigger: 'change' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  orderNo: [
    { required: true, message: '采购单号不能为空', trigger: 'change' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  // region: [{ required: true, message: 'Please select Activity zone', trigger: 'change' }],
  // date1: [{ required: true, message: 'Please pick a date', trigger: 'change', type: 'object' }],
  // type: [
  //   {
  //     type: 'array',
  //     required: true,
  //     message: 'Please select at least one activity type',
  //     trigger: 'change',
  //   },
  // ],
  // resource: [{ required: true, message: 'Please select activity resource', trigger: 'change' }],
  // desc: [{ required: true, message: 'Please input activity form', trigger: 'blur' }],
};

var baseFormSchema = [
  {
    field: '',
    label: '',
    component: 'group',
    items: [
      {
        field: 'isHandBoole',
        label: '是否手动输入费用',
        required: true,
        component: 'Switch',
      },
      {
        field: 'mouldNameObj',
        label: '型号(选择虚拟件采购单)',
        required: true,
        component: 'SelectOrderIndex',
        colProps: {
          span: 11,
        },
        show: true,
      },
      {
        field: 'mouldName',
        label: '型号',
        required: false,
        component: 'Input',
        colProps: {
          span: 11,
        },
        show: false,
      },
    ],
  },
  {
    field: '',
    label: '',
    component: 'text',
  },
  {
    field: '',
    label: '',
    component: 'group',
    items: [
      {
        field: 'materialName',
        label: '物料名称',
        component: 'Input',
        required: true,
        placeholder: '请输入物料名称',
        colProps: {
          span: 13,
          offset: 2,
        },
      },
      {
        field: 'specification',
        label: '物料规格',
        component: 'Input',
        required: true,
        placeholder: '请输入物料规格',
        colProps: {
          span: 13,
          offset: 2,
        },
      },
    ],
  },

  // {
  //   field: '',
  //   label: '',
  //   component:"group",
  //   items:[
  //     {
  //       field: 'materialNo',
  //       label: '物料编号',
  //       component: 'Input',
  //       colProps: {
  //         span: 13, offset: 2
  //       },
  //
  //     },
  //     {
  //       field: 'orderNo',
  //       label: '采购单号',
  //       component: 'Input',
  //
  //       colProps: {
  //         span: 13, offset: 2
  //       },
  //     },
  //   ]
  // },
  {
    field: 'materialNo',
    label: '物料编号',
  },
  {
    field: 'orderNo',
    label: '采购单号',
  },
  {
    field: 'mouldName',
    label: '费用名称',
  },
  {
    field: 'model',
    label: '物料型号',
  },
  {
    field: '',
    label: '',
    component: 'group',
    items: [
      {
        field: 'price',
        label: '金额',
        required: true,
        component: 'InputNumber',
        colProps: {
          span: 13,
          offset: 2,
        },
      },
      {
        label: '供应商',
        field: 'supplierObj',
        colProps: {
          span: 13,
          offset: 2,
        },
        component: 'SelectSupplierIndex',
        required: true,
      },
    ],
  },

  {
    field: 'supplierName',
    label: '供应商名称',
  },
  {
    field: 'materialNos',
    label: '物料虚拟件',
  },

  {
    field: 'isHand',
    label: '是否手动输入',
  },
  // {
  //   label: '费用类型',
  //   field: 'costTypeId',
  //   component: 'ApiSelect',
  //   componentProps: {
  //     api: dictAllListApi,
  //     params:{type:1},
  //     resultField:"data",
  //     labelField: 'name',
  //     valueField: 'id',
  //   },
  //   required: true,
  // },
  {
    label: '关联MS采购单物料(非虚拟件)',
    field: 'items',
    component: 'FormSelectTable',
    // rules: [{ required: true }],
    render: ({ model, field }) => {
      return h(FormSelectTable, {
        value: model[field],
        // value: model.owner_name,
        isAdd: false,
        mouldMainId: model.mouldMainId,
        mode: 'single', // multiple 多选 single 单选
        onChange: (e: ChangeEvent) => {
          model[field] = e;
        },
      });
    },
  },

  {
    field: '',
    label: '',
    component: 'group',
    items: [
      {
        field: 'startDate',
        label: '开始日期',
        required: true,
        component: 'DatePicker',
        componentProps: {
          valueFormat: 'YYYY-MM-DD',
          format: 'YYYY-MM-DD',
        },
        colProps: {
          span: 13,
          offset: 2,
        },
      },
      {
        field: 'endDate',
        label: '结束日期',
        component: 'DatePicker',
        componentProps: {
          valueFormat: 'YYYY-MM-DD',
          format: 'YYYY-MM-DD',
        },
        colProps: {
          span: 13,
          offset: 2,
        },
      },
      // {
      //   label: '人员类型',
      //   field: 'userType',
      //   required: true,
      //   component: 'Select',
      //   colProps: {
      //     span: 11,
      //   },
      //   componentProps: {
      //     options: [
      //       {label: '采购员', value: 1},
      //       {label: '研发制样员', value: 2},
      //
      //     ],
      //   },
      //
      // },
    ],
  },
  {
    field: '',
    label: '',
    component: 'group',
    items: [
      {
        label: '经办人',
        field: 'userName',
        component: 'Ainput',
        rules: [{ required: true }],
        colProps: {
          span: 13,
          offset: 2,
        },
      },
      {
        field: 'remark',
        label: '备注',
        component: 'Input',
        colProps: {
          span: 13,
          offset: 2,
        },
      },
    ],
  },
  {
    field: 'returnCondition',
    label: '',
    component: 'group',
    items: [
      {
        field: 'returnConditionType',
        label: '退款条件类别',
        required: true,
        component: 'Select',
        colProps: {
          span: 11,
        },
        componentProps: {
          options: [
            { label: '一次性(数量)', value: 1 },
            { label: '累计(数量)', value: 2 },
            { label: '一次性(金额)', value: 4 },
            { label: '累计(金额)', value: 5 },
            { label: '不退', value: 3 },
          ],
        },
      },
      {
        field: 'returnConditionNum',
        label: '退款条件数量',
        required: true,
        component: 'InputNumber',
        colProps: {
          span: 11,
        },
      },
    ],
  },
  {
    field: 'totalNum',
    label: '累计下单量',
    component: 'InputNumber',
    placeholder: '该累计下单量将与MS采购单数量相加来对比退款条件数量!',
    show: false,
  },
  {
    field: 'userId',
    label: '经办人id',
  },
];

export const formSchema = baseFormSchema;
